package com.rede.didiok.user.restapi;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.annotion.log.BussinessLog;
import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.dto.user.NoticeDto;
import com.rede.didiok.common.enums.BehaviorEnum;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.exception.exceptionType.QueryException;
import com.rede.didiok.common.holder.RequestHolder;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.common.validator.group.Delete;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.user.service.NoticeService;

/**
 * 通知 RestApi
 *
 * @author Rede
 * @date 2023年8月8日15:11:47
 */
@RestController
@RequestMapping("/notice")
@Api(value = "通知相关接口", tags = {"通知相关接口"})
@Slf4j
public class NoticeRestApi {

    @Autowired
    NoticeService noticeService;

    @BussinessLog(value = "获取通知列表", behavior = BehaviorEnum.GET_NOTICE)
    @ApiOperation(value = "获取通知列表", notes = "获取通知列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody NoticeDto noticeDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        String userUidStr = RequestHolder.getUserUid();
        if (StringUtils.isEmpty(userUidStr)) {
            throw new QueryException(Constants.USER_UID_EMPTY);
        }
        noticeDto.setCreateUserUid(userUidStr);
        noticeDto.setIsBack(0); // 前台通知
        return ResultUtil.successWithData(noticeService.getPageList(noticeDto));
    }

    @BussinessLog(value = "删除通知", behavior = BehaviorEnum.DELETE_NOTICE)
    @ApiOperation(value = "删除通知", notes = "删除通知", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody NoticeDto noticeDto, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        return noticeService.deleteNotice(noticeDto);
    }

    @BussinessLog(value = "删除选中通知", behavior = BehaviorEnum.DELETE_BATCH_NOTICE)
    @ApiOperation(value = "删除选中通知", notes = "删除选中通知", response = String.class)
    @PostMapping("/deleteBatch")
    public String deleteBatch(@Validated({Delete.class}) @RequestBody List<NoticeDto> noticeDtoList, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        return noticeService.deleteBatchNotice(noticeDtoList);
    }


    @ApiOperation(value = "获取用户收到的通知数", notes = "获取用户收到的通知数")
    @GetMapping("/getUserReceiveNoticeCount")
    public String getUserReceiveNoticeCount() {
        log.info("获取用户收到的评论回复数");
        return noticeService.getUserReceiveNoticeCount();
    }

    @ApiOperation(value = "阅读用户接收的通知数", notes = "阅读用户接收的通知数")
    @PostMapping("/readUserReceiveNoticeCount")
    public String readUserReceiveNoticeCount() {
        log.info("阅读用户接收的评论数");
        return noticeService.readUserReceiveNoticeCount();
    }

    @ApiOperation(value = "用户阅读某一类型的通知", notes = "用户阅读某一类型的通知")
    @PostMapping("/readNoticeByType")
    public String readNoticeByType(@RequestBody NoticeDto noticeDto) {
        log.info("用户阅读某一类型的通知");
        return noticeService.readNoticeByType(noticeDto);
    }

}

